import java.util.Random;
import java.util.Scanner;

/**
 * @author 10277
 */

public class GuessNumber {

    public static void main(String[]args){
        use();
    }
    public static void menu() {
        System.out.println("----欢迎游玩猜数字小游戏----");
        System.out.println("-----1.无限制次数玩法------");
        System.out.println("-----2.有限制次数玩法------");
        System.out.println("--------0.退出游戏--------");
        System.out.println("------------------------");
    }
    public static void game(){
        Random random=new Random();
        int guess=random.nextInt(10)+1;
        Scanner scanner=new Scanner(System.in);
        int i = 0;
        int number;
        while (true){
            System.out.println("请输入你猜到的数字");
            try {
                number=scanner.nextInt();
            }catch (Exception e){
                System.out.println("非法输入");
                break;
            }
            i=i+1;
            if (guess < number){
                System.out.println("猜大了");
            }else if (guess>number){
                System.out.println("猜小了");
            }else {
                System.out.println("恭喜你！猜对了"+"你一共猜了"+i+"次");
                break;
            }
        }
    }
    public static void games(){
        Random random=new Random();
        int guess=random.nextInt(10)+1;
        int number;
        System.out.println("你想限制次数为？");
        Scanner scanner=new Scanner(System.in);
        int fr=scanner.nextInt();
        for (int i = 0; i < fr; i++) {
            System.out.println("请输入你猜到的数字");
            try {
                number=scanner.nextInt();
            }catch (Exception e){
                System.out.println("非法输入");
                break;
            }
            if (i==fr-1) {
                if (guess != number) {
                    System.out.println("抱歉，次数用完 正确答案是"+guess);
                    break;
                }
            }
            if (guess < number){
                System.out.println("猜大了");
            }else if (guess>number){
                System.out.println("猜小了");
            }else {
                System.out.println("恭喜你！猜对了" + "你一共猜了" + (i+1)+ "次");
                break;
            }
        }
    }

    public static void use() {
        Scanner scanner=new Scanner(System.in);
        int input;
        do{
            menu();
            System.out.println("请输入");
            try {
                input = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("非法输入");
                break;
            }
            switch (input) {
                case 1: {
                    game();
                    break;
                }
                case 0: {
                    System.out.println("游戏结束");
                    return;
                }
                case 2:{
                    games();
                    break;
                }
                default: {
                    System.out.println("请输入正确的数字");
                    break;
                }
            }
        }while (input>0);
    }
}
